<template>
    <div>
        <el-form :rules="rules"
                 v-loading="loading"
                 element-loading-text="正在登录..."
                 element-loading-spinner="el-icon-loading"
                 element-loading-background="rgba(0, 0, 0, 0.8)"
                 ref="loginForm"
                 :model="loginForm"
                 class="loginContainer">
            <h3 class="loginTitle">用户登录</h3>
            <el-form-item prop="userCode">
                <el-input type="text" auto-complete="false" v-model="loginForm.userCode" placeholder="请输入用户名">
                </el-input>
            </el-form-item>
            <el-form-item prop="passWord">
                <el-input type="password"  auto-complete="false" v-model="loginForm.passWord" placeholder="请输入密码">
                </el-input>
            </el-form-item>
            <!--            <el-form-item prop="code">-->
            <!--                <el-input type="text"  auto-complete="false" v-model="loginForm.code"-->
            <!--                          placeholder="点击图片更换验证码"-->
            <!--                          style="width: 250px;margin-right: 5px"-->
            <!--                >-->
            <!--                </el-input>-->
            <!--                <img :src="captchaUrl" @click="updateCaptcha">-->
            <!--            </el-form-item>-->
            <el-checkbox v-model="checked" class="loginRemember">记住我</el-checkbox>
            <el-button type="primary" style="width: 100%"
                       @click="submitLogin">登录</el-button>
        </el-form>
    </div>
</template>

<script>

    import {getRequest} from "../utils/api";
    import {formatRoutes} from "../utils/menus";
    import {initMenu} from "../utils/menus";
    import router from "../router";
    import store from "../store/index"

    export default {
        name: "Login",
        data(){
            return{
                //调一下后端的接口，点击更换验证码的时候去更换,确保验证码能够刷新
                //captchaUrl:'/captcha?time='+new Date(),
                loginForm:{
                    userCode:'zs',
                    passWord:'123',
                    //code:''
                },
                loading:false,
                checked:true,
                rules:{
                    //from表单上面的prop
                    userCode:[{required:true,message:'请输入用户名',trigger:'blur'}],
                    passWord:[{required: true,message:'请输入密码',trigger:'blur'}],
                    // code:[{required:true,message:'请输入验证码',trigger:'blur'}],
                }
            }
        },
        methods:{
            // updateCaptcha(){
            //     this.captchaUrl = '/captcha?time='+new Date();
            // },
            submitLogin() {
                this.$refs.loginForm.validate((valid) => {
                    if (valid) {
                        this.loading = true;
                        this.postRequest('/house/customer/login',this.loginForm)
                            .then(resp => {
                                if (resp) {
                                    this.loading = false;
                                    //存储用户token
                                    const  tokenUser = resp.data.token;
                                    //把后端传过来的token放在系统的sessionstorage
                                    window.sessionStorage.setItem('tokenStr',tokenUser)
                                    //console.log(window.sessionStorage.getItem('tokenStr')+"----------------")
                                    //不可以点击回退退回到登录页面
                                    //alert(JSON.stringify(resp))
                                    this.$router.replace('/home');

                                    this.getRequest('/house/customer/getCustomerInfo')
                                        .then(res => {
                                            //console.log(JSON.stringify(res)+"================")
                                            window.sessionStorage.setItem("tokenCust", JSON.stringify(res.data.customer))
                                             console.log(window.sessionStorage.getItem("tokenCust")+"123");
                                            getRequest('/listResource/' + res.data.customer.roleId)
                                                .then(data => {
                                                    //console.log(data)
                                                    if (data) {
                                                        //  console.log(data);
                                                        //格式化好的
                                                        let fmRoutes = formatRoutes(data);
                                                        //console.log(JSON.stringify(fmRoutes)+"111111111");
                                                        //添加到；路由中
                                                        initMenu(router, store);
                                                        for (let i = 0, length = fmRoutes.length; i < length; i += 1) {
                                                            const element = fmRoutes[i];
                                                            router.addRoute(element);
                                                        }
                                                        //alert(store.state.routes);
                                                        //把他们放到vuex里面
                                                        store.commit('initRoutes', fmRoutes)
                                                    }

                                                })
                                        })
                                }else{
                                    this.loading = false;
                                    this.$router.replace('/');
                                }

                            })
                    } else {
                        this.$message.error('请输入所有字段');
                        return false;
                    }
                });
            }
        }

    }
</script>

<style>
    .loginContainer{
        border-radius: 15px;
        background-clip: padding-box;
        margin: 180px auto;
        width: 350px;
        padding: 15px 35px 15px 35px;
        background: #fff;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #cac6c6;
    }
    .loginTitle{
        margin: 0px auto 48px auto;
        text-align: center;
    }
    .loginRemember{
        text-align: left;
        margin: 0px 0px 15px 0px;
    }
    .el-form-item__content{
        display: flex;
        align-items: center;
    }
</style>